﻿#ifndef _ASYNC_SERVER_H
#define _ASYNC_SERVER_H

#include <WinSock2.h>
#include <Windows.h>
#include "..\..\include\common\NET_CMD.h"
#include "..\..\include\common\CDebug.h"
#include <list>
#include <algorithm>
#pragma comment(lib, "Ws2_32.lib")

using namespace std;

//常量定义
#define WM_SOCKET WM_USER + 101

struct CLIENT_INFO
{
	char username[MAX_USERNAME_LEN];
	SOCKET s;
	SYSTEMTIME LoginTime;
	char skey[CLIENTKEY_LEN];
	int nHeartPacketTime;

	CLIENT_INFO()
	{
		memset(username, 0, MAX_USERNAME_LEN);
		memset(skey, 0, CLIENTKEY_LEN);
		s = INVALID_SOCKET;
		nHeartPacketTime = HEART_TIMEOUT;
	}
};

class CAsyncServer
{
public:
	CAsyncServer();
	~CAsyncServer();

public:
	//开启服务
	BOOL Start(ULONG ulServer, UINT uPort, HWND hWnd);
	//停止服务
	void Stop();
	//添加成员到列表
	void AddClient(CLIENT_INFO client);
	//删除Client
	void DelClient(SOCKET &s);
	//查询Client信息
	void QueryClient(const SOCKET &s, CLIENT_INFO *client);

public:
	//客户端列表
	list<CLIENT_INFO> m_ClientList;

private:
	//初始化Winsock库
	BOOL InitWinsock();

private:
	SOCKET m_sckListen;
	CDebug debug;
	UINT m_uId;
	HANDLE m_hAcceptThread;
};

#endif